/**
 * 签到页面
 * author:yinht
 * date:2017年6月1日9:10:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");

	var memberShip = {
		/**
		 * bindEvent 页面事件
		 */
		
		type: "",    //判断页面显示隐藏
		laytype:"",   //加载接口用的
		bindEvent: function() {
			//返回
			$("#return_Manage").unbind().on("click", function() {
				var pk_member = window.localStorage.setItem("pk_member","");
				window.localStorage.setItem("laytype", "");
				window.history.back(-1);
			});
			$(".mem_cont").unbind().on("click", function() {
				$(".mem_cont").removeClass("actived_fo");
				$(this).addClass("actived_fo");
				$(".public").hide();
				$($(this).attr("datatype")).show();
				$(".genjin_contont").html("");
//				memberShip.type = $(this).attr("type");
				memberShip.laytype = $(this).attr("laytype");
				if($(this).attr("laytype")=="0"){
					memberShip.sendParam();
				}else if($(this).attr("laytype")=="1"){
					memberShip.followRemind();
				}
				console.log(memberShip.laytype);
				window.localStorage.setItem("laytype", memberShip.laytype);
				window.localStorage.setItem("type", memberShip.type);
			});
			//删除
			$(".deletemember").unbind().on("click", function() {
				
				memberShip.deletemember();
			});
			$("#share").unbind().on("click", function() {
				$(".delete_items").toggleClass("hidden");
			});
			$(".bianji").unbind().on("click", function() {
				window.sessionStorage.setItem("bianji","1");
				window.sessionStorage.setItem("update","编辑");
				window.location.href = "#/index/newmenber";
				
			});
			
			$(".xinzengfollow").unbind().on("click", function() {
				window.location.href = "#/index/newfollow";
//				var pk_member = $(this).attr("pk_member");
//				window.localStorage.setItem("pk_member", pk_member);
				window.localStorage.setItem("laytype", memberShip.laytype);
			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var from = "right"; //从右边过来
				if((page_name == "ManagementSup")||(page_name == "Management")){
					from = "left";
				}
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: from
				});
			});
			Path.listen();
		},
		sendParam:function(){
			var pk_member = window.localStorage.getItem("pk_member");
			var pk_user = window.localStorage.getItem("pk_user");
//			var listtype = "1";//0是申请 1是变更
			var type = "2";
			var param = {
				 pk_member:pk_member,
				 pk_user: pk_user,
				 supertype: type,
       			}
			common.loadData({
				url: "/service/RRTPubServlet?method=getMemberDetail", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: memberShip.getMemberDetailCallBack, //回调函数
				dataSource: "getMemberDetail",
				cache: false
			});
				
		},
		followRemind: function(){	
			var pk_user = window.localStorage.getItem("pk_user");
			var pk_member = window.localStorage.getItem("pk_member");
			var param = {
				pk_user:pk_user,
				pk_member:pk_member
			}
			common.loadData({
				url: "/service/RRTPubServlet?method=followRecord", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: memberShip.followRecordCallBack, //回调函数
				cache: false
			});
		},
		
		getMemberDetailCallBack: function(json) {
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				window.localStorage.setItem("userInfo",JSON.stringify(json));
				var addinput = "";
				var memohtml="";
				var rp="";
				var rc="";
				$.each(json.data , function(key, val) {
					if(val!=""){
						$("."+key).parent().removeClass("hidden");
						$("."+key).html(val);
						if(key=="address"){
							var address = val;
							$.each(address , function(key, val) {
								$("."+key).parent().removeClass("hidden");
								$("."+key).html(val);
								if(key=="receiverProvince"&& rc!=val){
									addinput+=val;
									rp=val;
								}
								if(key=="receiverCity" &&rp!=val){
									addinput+=val;
									rc=val;
								}
								if(key=="receiverRegion"){
									addinput+=val;
								}
							});
						}
						if(key=="memolabel"){
							if(val.length>0){
								var memolabel = val;
								for(var i = 0;i<memolabel.length;i++){
									$("."+key).parent().removeClass("hidden");
										if(key=="memolabel"){
											memohtml += memolabel[i];	
										}
								}
							}
						}
					}
				});
				$(".receiverProvince").html(addinput);
				$(".memo").html(memohtml);
				var iscroll = require("iscroll"); 
				common.initIscroll({
					container: "iscrollContent_memberShip",
					isVertical:true ,
					showScrollBar: true
			});
			} else {
				common.showTip({
					content: json.message
				});
			}
		
		},
		followRecordCallBack: function(json){
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				if(json.data && json.data.list && json.data.list.length > 0) {
				var html = "";
					for(var i = 0; i < json.data.list.length; i++) {
						html += " <div class='genjin_item'>" +
							"<div class='lf time_item'>" +
							"<span>"+json.data.list[i].time+"</span>" +
							"<img src='images/dot.png' alt='' class='dot_img'>" +
							"</div>" +
							"<div class='lf con_item'>" +
							"<div class='st_item'>" +
							"<div class='gj_peo'>" +
							"<span class='gj_lt'>跟进人：</span><span class='gj_rg'>"+json.data.list[i].follow+"</span>" +
							"</div>" +
							"<div class='gj_style'>" +
							"<span class='gj_lt'>跟进方式：</span><span class='gj_rg'>"+json.data.list[i].gjstyle+"</span>" +
							"</div>" +
							"<div class='gj_detail'>" +
							"<span class='gj_lt1'>跟进详情：</span><span class='gj_rg'>"+json.data.list[i].gjdetails+"</span>" +
							"</div>" +
							"</div>" +
							"</div>" +
							"</div>";
					}
				}
				$(".genjin_contont").append(html);
			}else {
						common.showTip({
							content: json.message
						});
					}
		},
		deletemember: function(){
			var pk_member = window.localStorage.getItem("pk_member");
			var pk_user = window.localStorage.getItem("pk_user");
			var param = {
				 pk_member:pk_member,
				 pk_user: pk_user
       			}
			common.loadData({
				url:  "/service/RRTPubServlet?method=deleteMember", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: memberShip.deletememberCallBack, //回调函数
				dataSource: "followRemind",
				cache: false
			});
		},
		deletememberCallBack: function(json){
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				common.showTip({
				    time:3000,
				    content:"删除会员成功！",
				    callback:function(){
				    	//common.showTip({content:"我已经关闭"});
				    	window.history.back(-1);
				    	}
				});
				return ;
			} else {
				common.showTip({
					content: json.message
				});
			}	
		},
		/**
		 * 页面数据加载
		 */
		loadData: function() {
			var permissions = window.localStorage.getItem("permissions");
			if(permissions == "1"){
				$(".new_genjin span").css("display","none");
				$(".new_genjin").css("height","0");
				$(".new_genjin").removeClass("xinzengfollow");
				$(".plus_img").css("height","0");
			}
			
			var laytype;
			laytype = window.localStorage.getItem("laytype");
			if(!laytype||(laytype == "")){
				laytype = "0";
			}
			memberShip.laytype=laytype;
			$(".mem_cont").removeClass("actived_fo");
			$(".mem_cont").each(function(){
				if($(this).attr("laytype")==laytype){
					$(this).addClass("actived_fo");
					$(".public").hide();
					$($(this).attr("datatype")).show();
				}
				
			});
			memberShip.sendParam();
			memberShip.followRemind();
			var type = window.localStorage.getItem("type");
			var permissions = window.localStorage.getItem("permissions");
			if((permissions == "1")&&(type == "1")){
				$(".bianji").css("display","none");
			}
		},
		/**
		 * each page init mothed
		 */
		initialize: function() {

			this.loadData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = memberShip;
});	

